package com.kaifamiao.month04.day15;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayList implements Iterable {

    Object[] list = new Object[10];
    private int count = 0;
    final double threshold = 0.75;
    final double growth = 1.5;

    private int modCount = 0;

    /**
     * 扩容，阈值为 0.75 ，每次扩容 1.5 倍
     */
    public void grow() {
        if (count >= list.length * threshold) {
            list = Arrays.copyOf(list, (int) Math.round(list.length * growth));
        }
    }

    /**
     * 删除指定位置的元素
     *
     * @param index 需要删除元素的下标
     * @return 删除的元素内容
     */
    public Object remove(int index) {
        checkIndex(index);
        modCount++;
        Object s = get(index);
        System.arraycopy(list, index + 1, list, index, count - index - 1);
        list[count--] = null;
        return s;
    }

    /**
     * 检查下标是否正常
     *
     * @param index 下标
     */
    public void checkIndex(int index) {
        if (index < 0 || index >= count) {
            throw new RuntimeException("下标异常：" + index);
        }
    }

    /**
     * 查找指定位置元素
     *
     * @param index 需要查找的元素的位置
     * @return 该位置的元素
     */
    public Object get(int index) {
        checkIndex(index);
        return list[index];
    }

    /**
     * 给指定位置添加元素
     *
     * @param index   需要添加元素的位置
     * @param element 需要添加的元素内容
     */
    public void add(int index, Object element) {
        checkIndex(index);
        modCount++;
        grow();
        System.arraycopy(list, index, list, index + 1, list.length - index - 1);
        list[index] = element;
        count++;
    }

    /**
     * 默认给末尾添加元素
     *
     * @param element 需要添加的元素
     */
    public void add(Object element) {
        grow();
        modCount++;
        list[count++] = element;
    }

    /**
     * 获取某个元素的位置
     *
     * @param element 需要查找的元素
     * @return 该元素所在的位置
     */
    public int index(Object element) {
        for (int i = 0; i < list.length; i++) {
            if (element.equals(list[i])) {
                return i;
            }
        }
        return -1; // 未找到返回 -1
    }

    /**
     * 统计元素的数量
     *
     * @return 元素的数量
     */
    public int size() {
        return count;
    }

    @Override
    public Iterator iterator() {
        return new MyArrayListIterator();
    }

    private class MyArrayListIterator implements Iterator {

        private int current;
        private int index = -1;
        private int exceptedCount = modCount;

        @Override
        public boolean hasNext() {
            return current != count;
        }
        @Override
        public Object next() {
            checkExceptedCount();
            if (current >= count) {
                throw new NoSuchElementException();
            }
            return list[index = current++];
        }


        public void remove() {
            if (index == -1) {
                throw new IllegalStateException();
            }
            checkExceptedCount();
            ArrayList.this.remove(index);
            exceptedCount = modCount;
            current = index;
            index = -1;
        }

        private void checkExceptedCount() {
            if (exceptedCount != modCount) {
                throw new ConcurrentModificationException();
            }
        }
    }


}
